<?php
/**
 * @todo 未优化版本 
 *
 */

class ml_biz_opmark_parse
{
    private $_html;
    private $_func2value;
    public function start($string)
    {
        $this->_reset();
        $this->_html = $string;
        preg_match_all('/(@{ML[^}]*})/U' , $this->_html , $aRs);
        $aReturn = array();
        foreach ($aRs[1] as $row)
        {
            $data = substr($row , 4 , strlen($row)-5);
            $func = substr($data , 0 , strpos($data , ' '));
            parse_str(substr($data , strpos($data , ' ')+1) , $param);
            
            
            $aData = explode(' ' , substr($data , strpos($data , ' ')+1));
            $this->_func2value[$func][] = array(
                'replace' => $row,
                'param' => $param,
            );
        }
        

        return true ;
    }
    public function end()
    {
        return $this->_html;
    }
    public function func_rid2attN()
    {
        foreach ($this->_func2value['rid2attN'] as $row)
        {
            $rid = $row['param']['rid'];
            $aRid[] = $rid;
            $aRid2repl[$rid] = $row['replace'];
        }
        
        $oRds = new ml_model_rdsCount();
        $oRds->getAttitudeCount($aRid);
        $ar2n = $oRds->get_data();
        
        
        ksort($aRid2repl);
        ksort($ar2n);
        $replace = array_combine($aRid2repl , $ar2n);
        $this->_html = strtr($this->_html , $replace);
        return true;
    }
    public function func_rid2islike($uid , $html_yes , $html_no = '')
    {
        //未登录 可以直接清理OPMARK 并退出
        if(!$uid)
        {
            $this->_clean_opmark('rid2islike');
            return ;
        }
            
        foreach ($this->_func2value['rid2islike'] as $row)
        {
            $rid = $row['param']['rid'];
            $aRid[] = $rid;
            $aRid2repl[$rid] = $row['replace'];
        }

        $oIlike = new ml_model_dbILike();
        $rs = $oIlike->getListByUidRids($uid, $aRid);
        $aAttitude = Tool_array::format_2d_array($oIlike->get_data() , 'content_rid' , Tool_array::FORMAT_VALUE2VALUE2 ,'attitude');
        foreach ($aRid as $rid)
        {
            $html_repl = isset($aAttitude[$rid]) ? $html_yes : $html_no;
            $this->_html = str_replace($aRid2repl[$rid] , $html_repl , $this->_html);
        }
        
        return true;
    }
    
    private function _clean_opmark($func)
    {
        $this->_html = preg_replace('/(@{ML'.$func.'[^}]*})/U' , '' , $this->_html);
    }
    private function _reset()
    {
        $this->_html = '';
        $this->_func2value = array();
    }
    
}

/*
$a = '@{MLrid2attN rid=000f44cc010001juy}vxcvxcvx@{MLrid2attN rid=000f425b010001k91}vxcvxcvx@{MLrid2attN rid=000f432e010001k0j}';
$o = new ml_biz_opmark_parse();
$rs = $o->parse($a)
*/